library(ggplot2)
library(dplyr)

survey_long <- readRDS("data/df_long.rds")

############ Figure 3: Distance-Feeling Plots by countries

gb <- ggplot(survey_long %>% filter(close_relationship != "" & dem_country_code == "GB"  & d_ownparty == 0) %>% 
               group_by(close_relationship2) %>%
               summarise(feeling_toward = weighted.mean(feeling_toward, weights = meta_Weight))) +
  geom_bar(aes(feeling_toward, y = as.factor(close_relationship2)), 
           position = "dodge", stat = "summary", fun = "mean") +
  ylab("") +
  xlim(c(0,7)) +
  ggtitle(paste0("United Kingdom (", 
                round(lm(close_num ~ feeling_toward, survey_long %>% 
                     filter(dem_country_code == "GB"))$coefficient[2], 2), ")")) +
  xlab("Average feeling thermometer \n value") +
  theme_bw() + 
  theme(text= element_text(size = 16))

nl <- ggplot(survey_long %>% filter(close_relationship2 != "" & dem_country_code == "NL"  & d_ownparty == 0)  %>% 
               group_by(close_relationship2) %>%
               summarise(feeling_toward = weighted.mean(feeling_toward, weights = meta_Weight))) +
  geom_bar(aes(feeling_toward, y = as.factor(close_relationship2)), 
           position = "dodge", stat = "summary", fun = "mean") +
  ylab("") +
  xlim(c(0,7)) +
  ggtitle(paste0("Netherlands (", 
                round(lm(close_num ~ feeling_toward, survey_long %>% 
                           filter(dem_country_code == "NL"))$coefficient[2], 2), ")")) +
  xlab("Average feeling thermometer \n value") +
  theme_bw() + 
  theme(text= element_text(size = 16))


it <- ggplot(survey_long %>% filter(close_relationship2 != "" & dem_country_code == "IT"  & d_ownparty == 0)  %>% 
               group_by(close_relationship2) %>%
               summarise(feeling_toward = weighted.mean(feeling_toward, weights = meta_Weight))) +
  geom_bar(aes(feeling_toward, y = as.factor(close_relationship2)), 
           position = "dodge", stat = "summary", fun = "mean") +
  xlim(c(0,7)) +
  ylab("") +
  ggtitle(paste0("Italy (", 
                round(lm(close_num ~ feeling_toward, survey_long %>% 
                           filter(dem_country_code == "IT"))$coefficient[2], 2), ")")) +
  xlab("Average feeling thermometer \n value") +
  theme_bw() + 
  theme(text= element_text(size = 16))


pl <- ggplot(survey_long %>% filter(close_relationship2 != "" & dem_country_code == "PL"  & d_ownparty == 0) %>% 
               group_by(close_relationship2) %>%
               summarise(feeling_toward = weighted.mean(feeling_toward, weights = meta_Weight))) + 
  geom_bar(aes(feeling_toward, y = as.factor(close_relationship2)), 
           position = "dodge", stat = "summary", fun = "mean") +
  ylab("") +
  xlim(c(0,7)) +
  ggtitle(paste0("Poland (", 
                round(lm(close_num ~ feeling_toward, survey_long %>% 
                           filter(dem_country_code == "PL"))$coefficient[2], 2), ")")) +
  xlab("Average feeling thermometer \n value") +
  theme_bw()   + 
  theme(text= element_text(size = 16))

us <- ggplot(survey_long %>% filter(close_relationship2 != "" & dem_country_code == "US"  & d_ownparty == 0)  %>% 
               group_by(close_relationship2) %>%
               summarise(feeling_toward = weighted.mean(feeling_toward, weights = meta_Weight))) +
  geom_bar(aes(feeling_toward, y = as.factor(close_relationship2)), 
           position = "dodge", stat = "summary", fun = "mean") +
  ylab("") +
  xlim(c(0,7)) +
  ggtitle(paste0("United States (", 
                round(lm(close_num ~ feeling_toward, survey_long %>% 
                           filter(dem_country_code == "US"))$coefficient[2], 2), ")")) +
  xlab("Average feeling thermometer \n value") +
  theme_bw()  + 
  theme(text= element_text(size = 16))

fr <- ggplot(survey_long %>% filter(close_relationship2 != "" & dem_country_code == "FR"  & d_ownparty == 0)  %>% 
               group_by(close_relationship2) %>%
               summarise(feeling_toward = weighted.mean(feeling_toward, weights = meta_Weight))) +
  geom_bar(aes(feeling_toward, y = as.factor(close_relationship2)), 
           position = "dodge", stat = "summary", fun = "mean") +
  ylab("") +
  xlim(c(0,7)) +
  ggtitle(paste0("France (", 
                round(lm(close_num ~ feeling_toward, survey_long %>% 
                           filter(dem_country_code == "FR"))$coefficient[2], 2), ")")) +
  xlab("Average feeling thermometer \n value") +
  theme_bw()  + 
  theme(text= element_text(size = 16))

gr <- ggplot(survey_long %>% filter(close_relationship2 != "" & dem_country_code == "GR"  & d_ownparty == 0)  %>% 
               group_by(close_relationship2) %>%
               summarise(feeling_toward = weighted.mean(feeling_toward, weights = meta_Weight))) +
  geom_bar(aes(feeling_toward, y = as.factor(close_relationship2)), 
           position = "dodge", stat = "summary", fun = "mean") +
  ylab("") +
  ggtitle(paste0("Greece (", 
                round(lm(close_num ~ feeling_toward, survey_long %>% 
                           filter(dem_country_code == "GR"))$coefficient[2], 2), ")")) +
  xlim(c(0,7)) +
  xlab("Average feeling thermometer \n value") +
  theme_bw()+ 
  theme(text= element_text(size = 16))

de <- ggplot(survey_long %>% filter(close_relationship2 != "" & dem_country_code == "DE"  & d_ownparty == 0) %>% 
               group_by(close_relationship2) %>%
               summarise(feeling_toward = weighted.mean(feeling_toward, weights = meta_Weight))) +
  geom_bar(aes(feeling_toward, y = as.factor(close_relationship2)), 
           position = "dodge", stat = "summary", fun = "mean") +
  ylab("") +
  xlim(c(0,7)) +
  ggtitle(paste0("Germany (", 
                round(lm(close_num ~ feeling_toward, survey_long %>% 
                           filter(dem_country_code == "DE"))$coefficient[2], 2), ")")) +
  xlab("Average feeling thermometer \n value") +
  theme_bw()   + 
  theme(text= element_text(size = 16))

se <- ggplot(survey_long %>% filter(close_relationship2 != "" & dem_country_code == "SE"  & d_ownparty == 0) %>% 
               group_by(close_relationship2) %>%
               summarise(feeling_toward = weighted.mean(feeling_toward, weights = meta_Weight))) +
  geom_bar(aes(feeling_toward, y = as.factor(close_relationship2)), 
           position = "dodge", stat = "summary", fun = "mean") +
  ylab("") +
  xlim(c(0,7)) +
  ggtitle(paste0("Sweden (", 
                round(lm(close_num ~ feeling_toward, survey_long %>% 
                           filter(dem_country_code == "SE"))$coefficient[2], 2), ")")) +
  xlab("Average feeling thermometer \n value") +
  theme_bw()   + 
  theme(text= element_text(size = 16))

es <- ggplot(survey_long %>% filter(close_relationship2 != "" & dem_country_code == "ES"  & d_ownparty == 0) %>% 
               group_by(close_relationship2) %>%
               summarise(feeling_toward = weighted.mean(feeling_toward, weights = meta_Weight))) +
  geom_bar(aes(feeling_toward, y = as.factor(close_relationship2)), 
           position = "dodge", stat = "summary", fun = "mean") +
  ylab("") +
  xlim(c(0,7)) +
  ggtitle(paste0("Spain (", 
                round(lm(close_num ~ feeling_toward, survey_long %>% 
                           filter(dem_country_code == "ES"))$coefficient[2], 2), ")")) +
  xlab("Average feeling thermometer \n value") +
  theme_bw()   + 
  theme(text= element_text(size = 16))


countries_plot <- grid.arrange(de, es, fr, 
                               gb, gr, it,
                               nl, pl, se,
                               us, ncol = 2)

ggsave("figures/Figure3.jpeg", plot = countries_plot,
       height = 12, width = 9, dpi = 600)







